//
//  STPageContentView.swift
//  iPhone
//
//  Created by Toj on 11/17/22.
//

import UIKit

class STPageContentView: UIViewController {
    
    public var viewStyle = STPageContentViewStyle()
    
    public var selectedIndex = 0
    
    public var views: [STPageContentViewProtocol] = [] { didSet {
        collectionView.reloadData()
    } }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalTo(view)
        }
        
        flowLayout.offset = CGFloat(selectedIndex) * viewStyle.width
    }
    
    private lazy var flowLayout: STPageViewFlowLayout = {
        let layout = STPageViewFlowLayout()
        layout.scrollDirection = .horizontal
        // cell之间 垂直间距
        layout.minimumLineSpacing = 0
        // cell之间 水平间距
        layout.minimumInteritemSpacing = 0
        return layout
    }()
    
    private(set) lazy var collectionView: UICollectionView = {
        let view = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        view.backgroundColor = .clear
        view.scrollsToTop = false
        view.bounces = false // 回弹性
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.isPagingEnabled = true
        view.isPrefetchingEnabled = false
        view.contentInsetAdjustmentBehavior = .never
        view.register(UICollectionViewCell.cell_class, forCellWithReuseIdentifier: UICollectionViewCell.cell_identifier)
        view.dataSource = self
        view.delegate = self
        view.isScrollEnabled = false
        return view
    }()
    
    deinit {
        print("-->startimes: \(st_className) func: deinit")
    }
}

extension STPageContentView {
    
    func titleSelected(at index: Int) {
        guard views.count > selectedIndex else { return }
        guard index != selectedIndex else { return }

        selectedIndex = index
        let indexPath = IndexPath(item: index, section: 0)
        collectionView.scrollToItem(at: indexPath, at: .left, animated: false)
    }
}

// MARK: - 代理事件 UICollectionViewDataSource
extension STPageContentView: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return views.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UICollectionViewCell.cell_identifier, for: indexPath)
        cell.contentView.subviews.forEach { $0.removeFromSuperview() }
        children.forEach { $0.removeFromParent() }
        
        var contentView: UIView?
        let view = views[indexPath.row]
        if let childViewController = view as? UIViewController {
            contentView = childViewController.view
            addChild(childViewController)
        }
        else if let childView = view as? UIView {
            contentView = childView
        }
        
        if contentView != nil {
            cell.contentView.addSubview(contentView!)
            contentView!.snp.makeConstraints { make in
                make.edges.equalTo(cell.contentView)
            }
        }
        
        return cell
    }
}

// MARK: - 代理事件 UICollectionViewDelegateFlowLayout
extension STPageContentView: UICollectionViewDelegateFlowLayout {
    // cell 大小
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.frame.size
    }
}

// MARK: - STPageViewFlowLayout
class STPageViewFlowLayout: UICollectionViewFlowLayout {
    var offset: CGFloat = 0
    
    public override func prepare() {
        super.prepare()
       
        collectionView?.contentOffset = CGPoint(x: offset, y: 0)
    }
    
    deinit {
        print("-->startimes: \(st_className) func: deinit")
    }
}
